#include "app_mqtt.h"

static MQTTClient client;

// 初始化一个连接参数的模板
static MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;

// 初始化i一个消息的模板
static MQTTClient_message pubmsg = MQTTClient_message_initializer;

// 外部程序注册在消息队列模块的回调函数，该回调函数负责数据接收后的实际处理
static int (*callback_func)(char *data, int len);

// 失联回调
static void app_mqtt_on_connection_lost(void *context, char *cause)
{
    log_error("MQTTClient_onConnectionLost: %s\n", cause);
    MQTTClient_disconnect(client, 1000);
    MQTTClient_destroy(&client);
}

// 消息到达回调
static int app_mqtt_on_message_arrived(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
    // 1 要把message的数据内容提取出来
    char *data = message->payload;
    char data_len = message->payloadlen;
    if (callback_func(data, data_len) < 0) // 外部注册的回调函数 执行对接收的数据进行处理
    {
        return 0;
    }
    return 1;
}

// 发送成功回调
static void app_mqtt_on_message_delivered(void *context, MQTTClient_deliveryToken dt)
{
    log_info("MQTTClient_publishMessage success\n");
}
/**
 * @brief  初始化MQTT
 * @return 0:成功, -1:失败
 *
 * 1、 创建matt客户端  全局一个
 * 2、 客户端回调函数的注册 三种事件：失联事件、发送成功事件、接收到消息事件
 * 3、 mqttu客户端要连接服务端
 * 4、 mqtt要主动订阅服务的某个主题
 */
int app_mqtt_init(void)
{
    int ret;
    // 创建matt客户端  全局一个
    if ((MQTTClient_create(&client, MQTT_URL, MQTT_CLIENT_ID, MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTCLIENT_SUCCESS)
    {
        log_error("MQTTClient_create error\n");
        return -1;
    }

    // 客户端回调函数的注册 三种事件：失联事件、发送成功事件、接收到消息事件
    if ((MQTTClient_setCallbacks(client, NULL, app_mqtt_on_connection_lost, app_mqtt_on_message_arrived, app_mqtt_on_message_delivered)) != MQTTCLIENT_SUCCESS)
    {
        log_error("MQTTClient_setCallbacks error\n");
        MQTTClient_destroy(&client);
        return -1;
    }

    // mqttu客户端要连接服务端
    // if ((ret = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
    // {
    //     log_error("MQTTClient_connect error : %d\n", ret);
    //     MQTTClient_destroy(&client);
    //     return -1;
    // }

    ret = MQTTClient_connect(client, &conn_opts);
    if (ret != MQTTCLIENT_SUCCESS)
    {
        MQTTClient_destroy(&client);
        log_error("MQTTClient_connect error\r\n");
        return -1;
    }


    // mqtt要主动订阅服务的某个主题
    if ((MQTTClient_subscribe(client, MQTT_SUBSCRIBE_TOPIC, MQTT_QOS)) != MQTTCLIENT_SUCCESS)
    {
        MQTTClient_destroy(&client);

        log_error("MQTTClient_subscribe error\n");

        return -1;
    }


    return 0;
}

int app_mqtt_send(char *data, int len)
{
    // 设置要发送的消息
    pubmsg.payload = data;
    pubmsg.payloadlen = len;
    pubmsg.qos = MQTT_QOS;

    
    // 发送消息
    if ((MQTTClient_publishMessage(client, MQTT_PUBLISH_TOPIC, &pubmsg, NULL)) != MQTTCLIENT_SUCCESS)
    {
        log_error("MQTTClient_publishMessage error\n");
        return -1;
    }
    return 0;
}

int app_mqtt_register_callback(int (*callback)(char *data, int len))
{
    callback_func = callback;
    return 0;
}

void app_mqtt_destroy(void)
{
    MQTTClient_disconnect(client, 1000);
    MQTTClient_destroy(&client);
}
